#include "portalhud.h"

#include "color.h"

extern u8 digital_ttf[];
extern u32 digital_ttf_size;

extern u8 hud_cake_png[];
extern u8 hud_score_png[];
extern u8 hud_score_time_png[];

PortalHud::PortalHud()
{
	_textImage = new DrawableImage();
	_textImage2 = new DrawableImage();
	_textRender = new TextRender();

    _textRender->SetFont(digital_ttf, digital_ttf_size);

	_hudCakeImage  = new Image();
   if(_hudCakeImage->LoadImage(hud_cake_png, IMG_LOAD_TYPE_BUFFER)!= IMG_LOAD_ERROR_NONE)exit(0);
   
	_hudScoreImage = new Image();
   if(_hudScoreImage->LoadImage(hud_score_png, IMG_LOAD_TYPE_BUFFER)!= IMG_LOAD_ERROR_NONE)exit(0);
   
	_hudScoreTimeImage = new Image();
   if(_hudScoreTimeImage->LoadImage(hud_score_time_png, IMG_LOAD_TYPE_BUFFER)!= IMG_LOAD_ERROR_NONE)exit(0);
	
	_hudCakeSprite.SetImage(_hudCakeImage);
	_hudCakeSprite.SetPosition(500, 40);
	
	_hudScoreSprite.SetImage(_hudScoreImage);
	_hudScoreSprite.SetPosition(10, 40);
		
	_totalCakes = 0;
	_cakeObtained = 0;
	_portalsSpawned = 0;
	
	_gameMode = GameMode::PORTAL_COUNT;
	
	_textImage->CreateImage(640, 128);
	_textImage->SetFont(_textRender);
	_textImage->SetColor((GXColor){118,171,225,255});
	_textImage->SetSize(30);
	
	_textImage2->CreateImage(640, 128);
	_textImage2->SetFont(_textRender);
	_textImage2->SetColor((GXColor){118,171,225,255});
	_textImage2->SetSize(30);
}


PortalHud::~PortalHud()
{

}


long PortalHud::GetTimeElapsed()
{
   //return ((_msEnd.tv_sec - _ms.tv_sec) * 1000.0 + (_msEnd.tv_usec - _ms.tv_usec)/1000.0) + 0.5;
   return tic;
}


void PortalHud::UpdateTime()
{
   gettimeofday(&_msEnd, NULL);
   tic+=15;
}

void PortalHud::Render()
{
    
	_hudCakeSprite.Draw();
	
	if(_gameMode == GameMode::STORY)
	{
		_textImage->RenderText("%02d/%02d",_cakeObtained,_totalCakes);
		_textSprite.SetImage(_textImage->GetImage());
		_textSprite.SetPosition(550,30);
		_textSprite.Draw();
	}
	else if(_gameMode == GameMode::TIMED)
	{
		_hudScoreSprite.SetImage(_hudScoreTimeImage);
		_hudScoreSprite.Draw();
		UpdateTime();
		double elapsed = GetTimeElapsed();
		struct tm mytm;
		mytm.tm_sec = (int)(elapsed/1000.0)%60;
		mytm.tm_min = (elapsed/1000.0)/60.0;
		
		char timestr[40];
		
		long rem = ((elapsed - ((mytm.tm_sec)*1000) - ((mytm.tm_min)*60000))/1000)*100;
		
	    strftime(timestr, sizeof(timestr), "%M:%S", &mytm);
		
		_textImage->RenderText("%s:%02d                                %02d/%02d",timestr,rem, _cakeObtained,_totalCakes);
		_textSprite.SetImage(_textImage->GetImage());
		_textSprite.SetPosition(50,30);
		_textSprite.Draw();
	}
	else if(_gameMode == GameMode::PORTAL_COUNT)
	{
		_hudScoreSprite.SetImage(_hudScoreImage);
		_hudScoreSprite.Draw();
		_textImage->RenderText("%03d                                   %02d/%02d",_portalsSpawned,_cakeObtained,_totalCakes);
		_textSprite.SetImage(_textImage->GetImage());
		_textSprite.SetPosition(50,30);
		_textSprite.Draw();
		
	}
	
}
